var app = getApp()

Page({

  data: {
    username: '',
    password: '',
    loading: false,
    canIuse: wx.canIUse('button.open-type.getUserInfo')
  },

  //将输入的用户名存到缓存中
  getUsername: function(e) {
    wx.setStorageSync("username", e.detail.value)
  },

  //将输入的密码存到缓存中
  getPassword: function(e) {
    wx.setStorageSync("password", e.detail.value)
  },

  onLoad: function(options) {
    var username = wx.getStorageSync('username')
    var password = wx.getStorageSync('password')
    if (username !== '' && password !== ''){
      this.setData({
        username: username,
        password: password
      })
    }
  },

  //点击登录事件
  loginClick: function(e) {
    var that = this
    that.setData({
      loading: true
    })
    var username = wx.getStorageSync("username");
    var password = wx.getStorageSync("password");
    wx.request({
      url: 'http://139.199.192.171:8080/nanitao/login222.action',
      dataType: 'json',
      data: {
        username: username,
        password: password,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        app.globalData.header.Cookie = 'JSESSIONID='+res.data[0].sessionId
        if (res.data[0].islogin == '1') {
          var header = app.globalData.header
          //请求获取信息
          wx.request({
            url: 'http://139.199.192.171:8080/nanitao/getmessage.action',
            data: {
              username: username,
              password: password
            },
            method: 'GET',
            header: header,
            success: function(res) {
              var message = res.data;
              var identity = message[0].identity
              var name = message[0].name
              var isAdmin = message[0].isAdmin
              var classnum = message[0].classnum
              app.globalData.identity = identity
              app.globalData.name = name
              app.globalData.isAdmin = isAdmin
              app.globalData.classnum = classnum
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000,
                success:function(){
                  that.setData({
                    loading: false
                  })
                } 
              })
              wx.redirectTo({
                  url: '../index/index'
              })
            }
          })
        } else if (res.data[0].islogin == '0' && wx.getStorageSync("username") != '' && wx.getStorageSync("password") != '') {
          wx.clearStorageSync()
          that.setData({
            loading:false
          })
          wx.showToast({
            title: '账号或密码错误，请重新输入！',
            icon: 'none',
            duration: 1200
          })
        } else if (wx.getStorageSync('username') == '') {
          wx.clearStorageSync()
          that.setData({
            loading: false
          })
          wx.showToast({
            title: '账号不能为空！',
            icon: "none",
            duration: 1200
          })
        } else if (wx.getStorageSync('password') == '') {
          wx.clearStorageSync()
          that.setData({
            loading: false
          })
          wx.showToast({
            title: '密码不能为空！',
            icon: "none",
            duration: 1200
          })
        }
      },

      fail: function(res) {
        console.log("--------fail--------");
      }
    })
  }

})